<?php

namespace App\Admin\Controllers;

use App\Models\Department;
use App\Models\Staff;
use App\Models\Store;
use App\Models\StoreDetail;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Arr;

class StaffController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '员工';
    
    protected $status = [
    	1 => '在职',
	    2 => '离职'
    ];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Staff());
        
        $grid->column('store', '所属门店')->display(function ($value) {
        	return Arr::get($value, 'detail.name', '本部');
        });
        $grid->column('staff_no', __('Staff No'))->editable();
        $grid->column('name', "姓名");
        $grid->column('department.name', '部门');
        $grid->column('salary', __('Salary'));
        //$grid->column('picture', '免冠证件照')->image('', '50');
        $grid->column('status', __('Status'))->switch([
	        	'on' => ['value' => 1, 'text' => '在职', 'color' => 'success'],
		        'off' => ['value' => 2, 'text' => '离职', 'color' => 'danger']]);
        $grid->column('created_at', __('Created at'));
        
        $grid->disableExport(false);
        $grid->export(function ($export) {
            $export->only(['staff_no', 'name', 'salary', 'status']);
            $export->column('status', function ($value, $original) {
                return $this->status[$original];
            });
        });
        
        $grid->filter(function (Grid\Filter $filter){
            $filter->disableIdFilter();
            $filter->like('staff_no', __('Staff No'));
            $filter->like('name', "姓名");
            $filter->equal('store_id', '门店')->select(StoreDetail::setSelect());
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Staff::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('store_id', __('Store id'));
        $show->field('staff_id', __('Staff id'));
        $show->field('department_id', __('Department id'));
        $show->field('picture', __('Picture'));
        $show->field('register_steel', __('Register steel'));
        $show->field('id_card', __('Id card'));
        $show->field('health_card', __('Health card'));
        $show->field('job_card', __('Job card'));
        $show->field('salary', __('Salary'));
        $show->field('status', __('Status'));
        $show->field('created_at', __('Created at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Staff());
        
		$store = Arr::add(Store::all()->pluck('detail.name', 'id')->toArray(), '0', '总部');
		ksort($store);

        $form->select('store_id', __('Store id'))
	        ->options($store)
	        ->required()
	        ->default(0);
        $form->text('staff_no', __('Staff no'))
            ->append('<a type="button" id="get_number">生成编码</a>')
            ->setScript("
 $('#get_number').on('click', function() {
    $.ajax({
        type:'post',
        url: '".Route('staffNo')."',
        data:{
            _token:LA.token,
        },
        success: function (res) {
            $('#staff_no').val(res);
            toastr.success('生成成功');
        }
    });
});")->rules('required|unique:staffs,staff_no,{{id}}');
        $form->select('department_id', __('Department Name'))
	        ->options(Department::where('store_id', 0)->get()->pluck('name', 'id'))
            ->required();
        $form->text('name', '姓名')->required();
        $form->multipleImage('picture', '免冠证件照')->removable();
        $form->multipleImage('register_steel', '入职表')->removable();
        $form->multipleImage('id_card', '身份证')->removable();
        $form->multipleImage('health_card', '健康证')->removable();
        $form->multipleImage('job_card', '特种工作证')->removable();
        $form->currency('salary', __('Salary'))->symbol('￥');
        $form->switch('status', __('Status'))
	        ->states([
	        	'on' => ['value' => 1, 'text' => '在职', 'color' => 'success'],
		        'off' => ['value' => 2, 'text' => '离职', 'color' => 'danger']
	        ])->default(1);
        
        return $form;
    }

    public function staffNo($model = 'staffs', $key = 'staff_no')
    {
        return get_number_chang($model, $key);
    }
}